<?php

App::uses('AppController', 'Controller');

/**
 * Photos Controller
 *
 * @property Photo $Photo
 */
class PhotosController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public $helpers = array('Js');
    public $components = array('RequestHandler');
    
    public function index() {
        if ($this->request->is('post')) {
            $this->Photo->create();
            if ($this->Photo->save($this->request->data)) {
                if ($this->RequestHandler->isAjax()) {
                    $this->Photo->recursive = 0;
                    $photoGroups = $this->Photo->PhotoGroup->find('list');
                    $this->set(compact('photoGroups'));
                    $this->set('photos', $this->paginate());
                    $this->render('ijax', 'ajax');
                    $this->Session->setFlash(__('The photo group has been saved'));
                }
                $this->Session->setFlash(__('The photo has been saved'));
            } else {
                $this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
            }
        }
        $photoGroups = $this->Photo->PhotoGroup->find('list');
        $this->set(compact('photoGroups'));
        $this->Photo->recursive = 0;
        $this->set('photos', $this->paginate());
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Photo->id = $id;
        if (!$this->Photo->exists()) {
            throw new NotFoundException(__('Invalid photo'));
        }
        $this->set('photo', $this->Photo->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {

            $this->Photo->create();

            if ($this->Photo->save($this->request->data)) {
                $this->Session->setFlash(__('The photo has been saved'));
            } else {
                $this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
            }
        }
        $photoGroups = $this->Photo->PhotoGroup->find('list');
        $this->set(compact('photoGroups'));
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Photo->id = $id;
        if (!$this->Photo->exists()) {
            throw new NotFoundException(__('Invalid photo'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Photo->save($this->request->data)) {
                $this->Session->setFlash(__('The photo has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Photo->read(null, $id);
        }
        $photoGroups = $this->Photo->PhotoGroup->find('list');
        $this->set(compact('photoGroups'));
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Photo->id = $id;
        if (!$this->Photo->exists()) {
            throw new NotFoundException(__('Invalid photo'));
        }
        $Photo = $this->Photo->find('first', array('conditions' => array('Photo.id' => $id)));
        if (unlink(IMAGES . DS . $Photo['Photo']['name'])) {
            if ($this->Photo->delete()) {
                $this->Session->setFlash(__('Photo deleted'));
                $this->redirect(array('action' => 'index'));
            }
        }
        $this->Session->setFlash(__('Photo was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
